<template>
  <div>
    <!--面包屑导航区-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图-->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容"
                    v-model="queryInfo.query"
                    clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"/>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table
        :data="goodslist" border stripe>
        <el-table-column type="index"/>
        <el-table-column prop="goods_name" label="商品名称"/>
        <el-table-column prop="goods_price" label="商品价格（元）" width="100px"/>
        <el-table-column prop="goods_weight" label="商品重量" width="90px"/>
        <el-table-column prop="add_time" label="创建时间" width="160px">
          <template v-slot:="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑" placement="top-start">
              <el-button type="primary" icon="el-icon-edit" size="mini"
                         @click="editGoodsInfo(scope.row.goods_id)"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top-start">
              <el-button type="danger" icon="el-icon-delete" size="mini"
                         @click="removeGoodsById(scope.row.goods_id)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!--分页插件-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="pageSizes"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total" background>
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: "List",
    data() {
      return {
        queryInfo: {
          query: '',
          pagenum: 1,
          pagesize: 5
        },
        goodslist: [],
        total: 0,
        pageSizes: [5, 10, 20, 50],
      }
    },
    created() {
      this.getGoodsList()
    },
    methods: {
      //获取商品列表
      getGoodsList() {
        this.$store.dispatch('getGoodsList_', this.queryInfo).then(() => {
          if (this.$store.state.goods_state !== 200) {
            return this.$message.error("获取商品列表失败")
          }
          this.goodslist = this.$store.state.goods_list.goods
          this.total = this.$store.state.total
        })
      },

      //删除商品
      removeGoodsById(id) {
        this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$store.dispatch('deleteGoodsById_', id).then(() => {
            if (this.$store.state.goods_state !== 200) {
              return this.$message.error("删除失败")
            }
            this.getGoodsList()
            this.$message({type: 'success', message: '删除成功!'});
          })
        }).catch(() => {
          this.$message({type: 'info', message: '已取消删除'});
        })
      },
      //监听pagesize改变
      handleSizeChange(newPageSize) {
        this.queryInfo.pagesize = newPageSize
        this.getGoodsList()
      },
      handleCurrentChange(newPageNum) {
        this.queryInfo.pagenum = newPageNum
        this.getGoodsList()
      },
      //监听添加关闭
      addDialogClosed() {
        this.$refs.addGoodsRef.resetFields()
      },
      //编辑框关闭
      editDialogClosed() {
        this.$refs.editGoodsRef.resetFields()
      },
      //修改商品 TODO
      editGoodsInfo(id) {
        // this.$router.push({name: 'edit', params: {id: id}})
      },
      goAddPage() {
        this.$router.push('/goods/add')
      }

    }
  }
</script>

<style scoped>

</style>
